package com.cskaoyan.service.impl;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.MallBrand;
import com.cskaoyan.bean.pojo.MallBrandExample;
import com.cskaoyan.bean.vo.BrandCreateVo;
import com.cskaoyan.bean.vo.BrandUpdateVo;
import com.cskaoyan.mapper.MallBrandMapper;
import com.cskaoyan.service.BrandListService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @Description:品牌制造商
 * @Author: chaohe@com.cskaoyan
 * @Date: 2021/7/9
 * @Time: 8:49
 */
@Service
public class BrandListServiceImpl implements BrandListService {

    @Autowired
    MallBrandMapper brandMapper;
    /**
     * @Author chaohe
     * @description: 品牌制造商list回显
     * @Date 14:43 2021/7/10
     * url: http://localhost:8083/admin/brand/list
     */

    @Override
    public BaseData<MallBrand> list(BaseParmListBo baseParam) {
        //BaseParmListBo是用继承的方式避免参数报空

        //分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        Integer id = baseParam.getId();
        String name = baseParam.getName();

        MallBrandExample mallBrandExample = new MallBrandExample();
        MallBrandExample.Criteria criteria = mallBrandExample.createCriteria();

        criteria.andDeletedEqualTo(0);

        //判断name和id
        if (name != null ){
            criteria.andNameLike("%"+name+"%");
        }
        if (id != null && id != 0){
            criteria.andIdEqualTo(id);
        }

        //排序
        mallBrandExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MallBrand> mallBrands = brandMapper.selectByExample(mallBrandExample);
        //求总数
        int total = (int) brandMapper.countByExample(mallBrandExample);
        return BaseData.ok(mallBrands, total);

    }
    /**
     * @Author chaohe
     * @description: 品牌制造商的添加，
     * @Date 14:46 2021/7/10
     * url: http://localhost:8083/admin/brand/create
     */

    @Override
    public BrandCreateVo create( BrandCreateBo brandCreateBo) {

        MallBrand mallBrand = new MallBrand();
        mallBrand.setName(brandCreateBo.getName());
        mallBrand.setDesc(brandCreateBo.getDesc());
        mallBrand.setFloorPrice(brandCreateBo.getFloorPrice());
        mallBrand.setPicUrl(brandCreateBo.getPicUrl());

        //以当前时间为数据库数据加入时间
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        mallBrand.setAddTime(timestamp);
        mallBrand.setUpdateTime(timestamp);

        //注意用insertSelective是为了让其用默认的值，另一种insert不允许用默认值
        int i = brandMapper.insertSelective(mallBrand);
        BrandCreateVo brandCreateVo = null;


        if (i == 1) {
            Integer id = mallBrand.getId();
            MallBrand mallBrand1 = brandMapper.selectByPrimaryKey(id);
            brandCreateVo = new BrandCreateVo(mallBrand1.getId(), mallBrand1.getName(), mallBrand1.getDesc(), mallBrand1.getPicUrl(), mallBrand1.getFloorPrice(), mallBrand1.getAddTime(), mallBrand1.getUpdateTime());

        }
        return  brandCreateVo;
    }
    /**
     * @Author chaohe
     * @description: 品牌制造商的删除，注意是逻辑删除
     * @Date 14:49 2021/7/10
     * url: http://localhost:8083/admin/brand/delete
     */

    @Override
    public void delete(BrandBaseBo brandBaseBo) {
        //deleted为1表示删除，逻辑删除掉的
        MallBrand mallBrand = new MallBrand(brandBaseBo.getId(), brandBaseBo.getName(), brandBaseBo.getDesc(), brandBaseBo.getPicUrl(), brandBaseBo.getSortOrder(), brandBaseBo.getFloorPrice(), brandBaseBo.getAddTime(), brandBaseBo.getUpdateTime(), 1);
        MallBrandExample mallBrandExample = new MallBrandExample();
        MallBrandExample.Criteria criteria = mallBrandExample.createCriteria();

        criteria.andIdEqualTo(brandBaseBo.getId());

        brandMapper.updateByExample(mallBrand, mallBrandExample);
    }
    /**
     * @Author chaohe
     * @description: 品牌制造商的更新
     * @Date 14:50 2021/7/10
     * url: http://localhost:8083/admin/brand/update
     */

    @Override
    public BrandUpdateVo update(BrandUpdateBo brandUpdateBo) {
        MallBrand mallBrand = new MallBrand(brandUpdateBo.getId(), brandUpdateBo.getName(), brandUpdateBo.getDesc(), brandUpdateBo.getPicUrl(), brandUpdateBo.getSortOrder(), brandUpdateBo.getFloorPrice(), brandUpdateBo.getAddTime(), brandUpdateBo.getUpdateTime(), 0);
        MallBrandExample mallBrandExample = new MallBrandExample();
        MallBrandExample.Criteria criteria = mallBrandExample.createCriteria();

        criteria.andIdEqualTo(brandUpdateBo.getId());
        brandMapper.updateByExample(mallBrand, mallBrandExample);
        MallBrand mallBrand1 = brandMapper.selectByPrimaryKey(brandUpdateBo.getId());
        //回显的时候deleted设置为false
        BrandUpdateVo brandUpdateVo = new BrandUpdateVo(mallBrand1.getId(), mallBrand1.getName(), mallBrand1.getDesc(), mallBrand1.getPicUrl(), mallBrand1.getSortOrder(), mallBrand1.getFloorPrice(), mallBrand1.getAddTime(), mallBrand1.getUpdateTime(), false);

        return brandUpdateVo;
    }


}
